package com.ztf.shopping.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ztf.shopping.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/findName")
public class FindName extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        Object loginUser = request.getSession().getAttribute("loginUser");
        ObjectMapper om = new ObjectMapper();
        Map<String,String> map = new HashMap<String,String>();

        if (loginUser==null){
            //用户未登录  1是未登录 0是已登录
            map.put("loginMsg","1");
            map.put("msg","请先登录后尝试");
            om.writeValue(response.getOutputStream(),map);
        }else {
            User user = (User) loginUser;
            map.put("loginMsg","0");
            map.put("msg",user.getName());
            om.writeValue(response.getOutputStream(),map);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
